/**
 * Created by Dendy on 2015/10/9.
 *
 * @author Dendy
 * @date 2016/3/31
 */
(function ($, W) {
    var title = $('title').text();
    var tag = false;
    var msgCnt = 0;
    var msgOrderCnt = 0;
    var it;

    // 修改密码
    $('#updatePwd').click(function (e) {
        e.preventDefault();
        updateModal.show();
    });
    $('#updatePwd_btn').click(function () {
        validateForm(function () {
            SysTool.Ajax.ajaxJsonCall('user/uPwd', {
                pwd: updateModal.form.find('input[name=newPwd]').val()
            }, function (data) {
                updateModal.hide();
                PopMessage.success('密码修改成功，请重新登录！');
            });
        });
    });

    function validateForm(callback) {
        var oldPwd = updateModal.form.find('input[name=oldPwd]').val();
        var newPwd = updateModal.form.find('input[name=newPwd]').val();
        var confirmPwd = updateModal.form.find('input[name=confirmPwd]').val();
        if (!oldPwd || !newPwd || !confirmPwd) {
            PopMessage.warning('有未填写的信息或输入信息错误！');
            return false;
        }
        SysTool.Ajax.ajaxJsonCall('user/vPwd', {
            pwd: updateModal.form.find('input[name=oldPwd]').val()
        }, function (data) {
            if (data) {
                var newPwd = updateModal.form.find('input[name=newPwd]').val();
                var confirmPwd = updateModal.form.find('input[name=confirmPwd]').val();
                if (newPwd == confirmPwd) {
                    callback();
                } else {
                    PopMessage.warning('两次输入的密码不一致！');
                }
            } else {
                PopMessage.warning('原密码不正确！');
            }
        })
    }

    function UpdatePwdModal() {
        this.modal = $('#updatePwd_modal');
        this.form = $('#updatePwd_form');
    }

    UpdatePwdModal.prototype = {
        Constructor: UpdatePwdModal
    };
    UpdatePwdModal.prototype.show = function () {
        this.modal.modal('show');
    };
    UpdatePwdModal.prototype.hide = function () {
        this.modal.modal('hide');
        this.clear();
    };
    UpdatePwdModal.prototype.clear = function () {
        this.form.find('input[name=oldPwd]').val('');
        this.form.find('input[name=newPwd]').val('');
        this.form.find('input[name=confirmPwd]').val('');
    };

    // 系统消息
    var SysMessage = function () {

    };

    SysMessage.prototype = {
        Constructor: SysMessage
    };

    SysMessage.prototype.queryNotRead = function () {
        // 查询未读消息
        SysTool.Ajax.ajaxJsonCall('msg/querySt', {}, function (data) {
            var msgContentHtml = '';
            if (data) {
                msgCnt = data.length || 0;
                msgContentHtml += '<li class="dropdown-header">' +
                    '<i class="icon-envelope-alt"></i>' + msgCnt + ' 条新消息 <button class="btn btn-primary btn-minier" onclick="sysMessage.allRead()">全部已读</button></li>';
                for (var i = 0; i < data.length; i++) {
                    var msg = data[i];
                    var id = msg.id;
                    var t = msg.title;
                    var c = msg.content;
                    var type = msg.typeCode;
                    //订单通知，需要强制弹窗。
                    if (type =='12' || type =='13'){
                        msgOrderCnt ++;
                    }

                    var s = msg.createTime;
                    if (c && c.length > 20) {
                        c = c.substr(0, 20) + '...';
                    }
                    var h = '<li>' +
                        '<a href="' + app_path + '/msg/detail/' + id + '">' +
                        '<span class="msg-body" style="display: inline">' +
                        '<span class="msg-title">' +
                        '<span class="blue"></span>' + c + '</span>' +
                        '<span class="msg-time">' +
                        '<i class="icon-time"></i>' +
                        '<span>' + SysTool.Date.formatDate(s, 'yyyy-MM-dd hh:mm:ss') + '</span>' +
                        '</span>' +
                        '</span>' +
                        '</a>' +
                        '</li>';
                    msgContentHtml += h;
                }

                if (msgCnt !=0){
                    $('body').append('<embed id="snd" height=0 width=0 src="http://118.178.225.32/hmp_website/assets-lib/media/alert.wav" autostart=true>');
                    PopMessage.info("您有" + msgCnt +"条消息，请注意查看", "消息通知", 7000);
                }
                if (msgOrderCnt != 0){
                    BootBox.warning("您有" + msgOrderCnt +"条订单消息，请及时查看", "消息通知", 7000);
                }
            } else {
                msgCnt = 0;
            }
            console.log(msgCnt);
            msgContentHtml += '<li><a href = "' + app_path + '/msg/home" >查询所有消息<i class="icon-arrow-right"></i></a></li>';
            $('#msgCnt').html(msgCnt);
            $('#msgContainer').html(msgContentHtml);
            if (msgCnt > 0) {
                if (!it) {
                    it = setInterval(function () {
                        if (!tag) {
                            $('title').text('您有' + msgCnt + '条新消息');
                            tag = true;
                        } else {
                            $('title').text(title);
                            tag = false;
                        }
                    }, 3000);
                }
            } else {
                clearInterval(it);
                it = undefined;
                $('title').text(title);
            }


        });

    };

    SysMessage.prototype.allRead = function () {
        SysTool.Ajax.ajaxJsonCall('msg/allRead', {}, function (data) {
            if (data){
                var msgContentHtml = '<li class="dropdown-header">' +
                    '<i class="icon-envelope-alt"></i>0 条新消息</li>'+
                    '<li><a href = "' + app_path + '/msg/home" >查询所有消息<i class="icon-arrow-right"></i></a></li>';
                $('#msgCnt').html(0);
                $('#msgContainer').html(msgContentHtml);
                
                clearInterval(it);
                it = undefined;
                $('title').text(title);
            }
        });
    };

    SysMessage.prototype.queryNotReadTimer = function (during) {
        var that = this;
        setInterval(function () {
            that.queryNotRead();
        }, during);
    };



    /**
     * 系统菜单。
     * @constructor
     */
    function Menu() {
    }

    Menu.prototype = {
        Constructor: Menu
    };

    /**
     * 根据访问url查询匹配的菜单，并激活菜单样式。
     *
     * 匹配算法：根据url匹配，从全部到每次截取url最后的"/"后的部分继续匹配，直到匹配到或url全部匹配为止。
     * @returns {boolean}
     */
    Menu.prototype.activeMenu = function () {
        var url = window.location.href;
        url = url.replace("http://", '');
        var match = url.substr(url.indexOf("/"));
        var menuDiv = $('div.nav-list');
        var subMenu = menuDiv.find('ul.submenu a');

        while (true) {
            var $dom = _matchMenu(match);
            if (!!$dom) {
                _activeMenu($dom);
                return true;
            }
            match = match.substr(0, match.lastIndexOf("/"));
            if (match.lastIndexOf("/") == 0) // 匹配到头了
                return false;
        }

        function _matchMenu(key) {
            for (var i = 0; i < subMenu.length; i++) {
                var menu = $(subMenu[i]);
                var href = menu.attr('href');
                if (href && href.startsWith(key)) {
                    return menu;
                }
            }
            return undefined;
        }

        function _activeMenu($d) {
            $d.parents('li').addClass('active');
            $d.parents('ul.submenu').css('display', 'block');
            $d.parents('ul.submenu').parent('li').addClass('open');
        }
    };

    var updateModal = new UpdatePwdModal();
    window.sysMessage = new SysMessage();
    sysMessage.queryNotRead();
    // 30秒查询一次未读消息
    sysMessage.queryNotReadTimer(1000 * 30);
    var menu = new Menu();
    menu.activeMenu();

})(window.jQuery, window);

